Conditions | 1 |
Paths | 1 |
Total Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
190 | function Context (lvl, writer) { |
||
191 | var self = this |
||
192 | var writers |
||
193 | var levels |
||
194 | |||
195 | function reset (lvl, writer) { |
||
196 | writers = new TreeNode(writer) |
||
197 | levels = new TreeNode(Level.find(lvl) || Level.Info) |
||
198 | } |
||
199 | |||
200 | reset(lvl, writer) |
||
201 | |||
202 | this.reset = reset |
||
203 | |||
204 | function path (name) { |
||
205 | return (name || '').toString().split('.').filter(function (_) { return _ }) |
||
206 | } |
||
207 | |||
208 | this.path = path |
||
209 | |||
210 | /** |
||
211 | * Retrieves level for provided logger |
||
212 | * |
||
213 | * @param {string} name |
||
214 | * @return {Level} |
||
215 | */ |
||
216 | this.getLevel = function (name) { return levels.retrieve(path(name)) } |
||
217 | |||
218 | /** |
||
219 | * Retrieves level for provided logger |
||
220 | * |
||
221 | * @deprecated |
||
222 | * |
||
223 | * @param {string} name |
||
224 | * @return {Level} |
||
225 | */ |
||
226 | this.getThreshold = this.getLevel |
||
227 | |||
228 | /** |
||
229 | * Sets level for specified logger |
||
230 | * |
||
231 | * @param {string} [name] |
||
232 | * @param {Level} lvl |
||
233 | */ |
||
234 | this.setLevel = function (name, lvl) { |
||
235 | if (!lvl) { |
||
236 | lvl = name |
||
237 | name = null |
||
238 | } |
||
239 | levels.put(path(name), Level.find(lvl)) |
||
240 | return self |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Sets level for specified logger |
||
245 | * |
||
246 | * @deprecated |
||
247 | * |
||
248 | * @param {string} [name] |
||
249 | * @param {Level} level |
||
250 | */ |
||
251 | this.setThreshold = this.setLevel |
||
252 | |||
253 | /** |
||
254 | * Removes and returns level at provided path |
||
255 | * |
||
256 | * @param {string} name |
||
257 | * @return {Level|undefined} |
||
258 | */ |
||
259 | this.removeLevel = function (name) { |
||
260 | var segments = path(name) |
||
261 | if (segments.length === 0) { |
||
262 | throw new Error('You can\'t remove default writer') |
||
263 | } |
||
264 | return levels.remove(segments) |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param {string} name |
||
269 | * |
||
270 | * @return {IWritable} |
||
271 | */ |
||
272 | this.getWriter = function (name) { return writers.retrieve(path(name)) } |
||
273 | |||
274 | /** |
||
275 | * @param {string} [name] Logger name |
||
276 | * @param {IWritable} writer |
||
277 | */ |
||
278 | this.setWriter = function (name, writer) { |
||
279 | if (!writer) { |
||
280 | writer = name |
||
281 | name = null |
||
282 | } |
||
283 | writers.put(path(name), writer) |
||
284 | return self |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Removes and returns writer under provided path |
||
289 | * |
||
290 | * @throws If attempted to remove root writer |
||
291 | * |
||
292 | * @param {string} name |
||
293 | * @return {IWritable|undefined} |
||
294 | */ |
||
295 | this.removeWriter = function (name) { |
||
296 | var segments = path(name) |
||
297 | if (segments.length === 0) { |
||
298 | throw new Error('You can\'t remove default writer') |
||
299 | } |
||
300 | return writers.remove(segments) |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @abstract |
||
305 | * @function Context#create |
||
306 | * @param {string} name |
||
307 | * @param {Level} [threshold] |
||
308 | * @param {IWritable} [writer] |
||
309 | * @return {T} |
||
310 | */ |
||
311 | } |
||
312 | |||
321 |